<template>
    <div class="article-search">
        <el-form :inline="true" :model="queryInfo" class="form-inline">
            <el-form-item label="分类名称">
                <el-input v-model="queryInfo.name" placeholder="请输入分类名称" style="width: 170px;" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getList">搜索</el-button>
                <el-button icon="Delete" @click="reset()">重置</el-button>
                <el-button type="primary" icon="CirclePlusFilled" @click="addForm()">添加</el-button>
            </el-form-item>
        </el-form>
    </div>
    <div class="article-table">
        <el-table :data="tableData" style="width: 1200px; height: 500px; overflow-x: auto !important; margin: 0 auto;">
          <el-table-column label="序号" width="50">
            <template #default="{ $index }">
              {{ $index + 1 }}
            </template>
          </el-table-column>
            <el-table-column prop="name" label="分类名称" width="150"/>
            <el-table-column fixed="right" label="操作">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="modify(scope.row)">
                    修改
                    </el-button><br/>
                    <el-button link type="primary" icon="Delete" @click="del(scope.$index)">
                    删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
          style="width: 1200px; margin: 20px auto 0 auto;"
          v-model:current-page="queryInfo.pageNum"
          v-model:page-size="queryInfo.pageSize"
          :page-sizes="[3, 5, 10, 20]"
          size="default"
          background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalNum"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
    </div>

    <el-dialog 
      v-model="addFormVisible" 
      :title="dialogType==0?'添加':'修改'" 
      width="500"
      @close="cancel"
      >
        <el-form :model="typeInfor">
        <el-form-item label="分类名称">
            <el-input v-model="typeInfor.name" autocomplete="off" placeholder="请输入分类名" />
        </el-form-item>
        </el-form>
        <template #footer>
        <div class="dialog-footer">
            <el-button @click="cancel2">取消</el-button>
            <el-button type="primary" @click="addOrModifyInfor()">
              {{ dialogType==0?'添加':'修改' }}
            </el-button>
        </div>
        </template>
    </el-dialog>
</template>

<script setup>
import {ref,onMounted } from 'vue'
import {ElMessage , ElMessageBox } from 'element-plus'
import axios from 'axios'
const addFormVisible = ref(false)


const totalNum = ref(0);

const dialogType = ref(0);

const delUrl = ref("");

const typeList = ref([]);
const consultList = ref([]);

// 定义一个方法当页面加载后立马执行的代码
onMounted(()=>{
    // console.log('onMounted');
    getList();
})

// 定义一个全局的查询参数变量
const queryInfo = ref({
    pageNum:'',
    pageSize: 5,
    name:'',
})


//重置并刷新
function reset() {
    queryInfo.value.name = '';
    getList();
}

// 定义一个方法，当页码发生变化时，调用后端接口，获取文章列表
const handleCurrentChange = (val)=>{
    queryInfo.value.pageNum = val;
    getList();
}

// 定义一个方法，当每页显示的条数发生变化时，调用后端接口，获取文章列表
const handleSizeChange = (val)=>{
    queryInfo.value.pageSize = val;
    queryInfo.value.pageNum = 1;
    getList(); 
}


const getList = ()=>{
    // 使用axios调用后端接口获取数据
    axios.get('/type/selectAll',{
        params: queryInfo.value
    })
    .then(resp=>{
        //console.log(resp.data);
        let result = resp.data;
        if(result.code==1){
            tableData.value = result.data.list;  
            //console.log(result.data.list);
            
            //console.log(tableData.value);
            
            totalNum.value = result.data.total;
            getInforList();
            //console.log('获取文章列表成功');      
        }else{
            console.log('获取列表失败')
        }
    })
    
}


const tableData = ref([{
  }])

const typeInfor = ref(
  {
    id: 0,
    name: '',
  }
)


function addForm() {
  dialogType.value = 0;
  typeInfor.value.name = '';
  addFormVisible.value=true;
}

function modify(row) {
  dialogType.value = 1;
  typeInfor.value = row;
  //console.log(2222,typeInfor.value);
  
  addFormVisible.value=true;
}

function addOrModifyInfor() {
  console.log(1111,typeInfor.value);
  
  if(dialogType.value==0){
    axios.post('/type/addType',typeInfor.value) 
    .then(resp=>{
        // console.log(resp.data)
        let result =  resp.data;  // resp是promise对象，.data返回我们自己的标准R
        if(result.code==1){          
            // 重新获取列表
            getList(); 
        } else{
            console.log('添加类型失败')
        }
    })
  }else{
    axios.put('/type/updateType',typeInfor.value) 
   .then(resp=>{
        // console.log(resp.data)
        let result =  resp.data;  // resp是promise对象，.data返回我们自己的标准R
        if(result.code==1){
            // 重新获取列表
            getList(); 
        } else{
            console.log('修改文章失败') 
        }
   })
  }
  typeInfor.value = {
    id: 0,
    name: '',
  }
  addFormVisible.value=false;
}

function delImgInPan(){
  console.log(delUrl.value);
  axios.delete('http://localhost/file/delete',{params: {fileName: delUrl.value}})
}

function cancel2() {
  delImgInPan();  
  addFormVisible.value=false;
}

function cancel() {
  getList();
  typeInfor.value = {
    id: 0,
    name: '',
  }
}

function del(n) {
  let id = tableData.value[n].id;

  delUrl.value = tableData.value[n].imageUrl;
  ElMessageBox.confirm(
    '你确定要删除吗？',
    '确定',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      center: true,
    }
  )
    .then(() => {
      // tableData.value.splice(n,1);
      // for(let i =0; i<tableData.value.length; i++){
      //   tableData.value[i].num = i+1;
      // }
      // ElMessage({
      //   type: 'success',
      //   message: '删除成功！',
      // })
      // 调用后端接口删除
      axios.delete('/type/'+id)
        .then(resp=>{
            // console.log(resp.data)
            let result =  resp.data;  // resp是promise对象，.data返回我们自己的标准R 
            if(result.code==1){
              delImgInPan();
              // 重新获取列表
              getList(); 
              ElMessage({
                  message: '删除成功！',
                  type: 'success',
              })
            }else{
              ElMessage({
                  message: '删除失败！',
                  type: 'warning',
              })
            }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}

</script>

<style scoped>
.article-search{ width: 800px; margin: 30px;}
.article-table{ z-index: 0;position: relative;}

.avatar-uploader .el-upload {
  border: 1px dashed black;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 160px;
  height: 160px;
  text-align: center;
}
.avatar-uploader .avatar {
  width: 160px;
  height: 160px;
  display: block;
}

</style>